import sys
from PyQt5.QtGui import QIcon, QPixmap, QFont, QPalette, QColor
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (QApplication, QWidget, QVBoxLayout, QLabel,
                             QFrame, QPushButton, QSpacerItem, QSizePolicy, QScrollArea)
import requests
from register import Register
from login import Login
from admin_login import Admin_Login
class Origin(QWidget):
    def __init__(self):
        super().__init__()
        # 设置中文字体支持
        font = QFont()
        font.setFamily("SimHei")  # 使用黑体显示中文
        font.setPointSize(10)
        self.setFont(font)

        self.setWindowTitle('学籍管理系统')
        try:
            icon = QIcon('../static/image/logo.png')
            self.setWindowIcon(icon)
        except:
            pass

        self.resize(450, 700)
        palette = QPalette()
        palette.setColor(QPalette.Window, QColor(245, 245, 245))
        self.setPalette(palette)

        main_layout = QVBoxLayout(self)
        # 设置布局间距
        main_layout.setSpacing(20)
        main_layout.setContentsMargins(50, 80, 50, 80)  # 左右边距50，上下边距80

        self.initUI(main_layout)

    def initUI(self, main_layout):
        # 添加Logo
        logo = QLabel()
        try:
            img = QPixmap('../static/image/logo.png')
            img = img.scaled(200, 200, Qt.KeepAspectRatio, Qt.SmoothTransformation)  # 平滑缩放
            logo.setPixmap(img)
        except:
            # 如果logo加载失败，显示文字提示
            logo.setText("学籍管理系统")
            logo.setFont(QFont("SimHei", 24, QFont.Bold))
            logo.setStyleSheet("color: #2c3e50;")

        logo.setAlignment(Qt.AlignCenter)
        main_layout.addWidget(logo)

        # 添加标题文字
        title = QLabel('请选择你的操作')
        title.setFont(QFont("SimHei", 16, QFont.Bold))  # 加粗字体
        title.setStyleSheet("color: #34495e; margin: 20px 0px;")  # 文字颜色和边距
        title.setAlignment(Qt.AlignCenter)
        main_layout.addWidget(title)

        # 添加间隔
        spacer1 = QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding)
        main_layout.addItem(spacer1)

        # 创建按钮容器
        frame = QFrame()
        frame.setStyleSheet("background-color: white; border-radius: 10px; box-shadow: 0 4px 8px rgba(0,0,0,0.1);")
        frame.setMinimumHeight(300)
        main_layout.addWidget(frame)

        # 按钮布局
        layout1 = QVBoxLayout(frame)
        layout1.setSpacing(15)
        layout1.setContentsMargins(30, 30, 30, 30)

        # 管理员登录按钮
        admin_btn = QPushButton('管理员登录')
        self.style_button(admin_btn, "blue", "#2980b9")  # 蓝色系按钮
        layout1.addWidget(admin_btn)
        admin_btn.clicked.connect(self.admin_btn_clicked)


        # 注册按钮
        register_but = QPushButton('注册')
        self.style_button(register_but, "#3498db", "#2980b9")  # 蓝色系按钮
        layout1.addWidget(register_but)
        register_but.clicked.connect(self.register)

        # 登录按钮
        login_but = QPushButton('登录')
        self.style_button(login_but, "#2ecc71", "#27ae60")  # 绿色系按钮
        layout1.addWidget(login_but)
        login_but.clicked.connect(self.login)

        # 退出按钮
        exit_but = QPushButton('退出')
        self.style_button(exit_but, "#e74c3c", "#c0392b", True)  # 红色系按钮
        layout1.addWidget(exit_but)
        exit_but.clicked.connect(self.close)  # 绑定退出功能

        # 添加间隔
        spacer2 = QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding)
        main_layout.addItem(spacer2)

        # 底部版权信息
        footer = QLabel('© 2025 学籍管理系统 版权所有')
        footer.setFont(QFont("SimHei", 9))
        footer.setStyleSheet("color: #7f8c8d;")
        footer.setAlignment(Qt.AlignCenter)
        main_layout.addWidget(footer)

    def style_button(self, button, bg_color, hover_color, is_danger=False):
        """设置按钮样式的通用方法"""
        button.setMinimumHeight(50)
        button.setFont(QFont("SimHei", 12, QFont.Bold))
        button.setStyleSheet(f"""
            QPushButton {{
                background-color: {bg_color};
                color: white;
                border-radius: 8px;
                padding: 10px;
                border: none;
            }}
            QPushButton:hover {{
                background-color: {hover_color};
            }}
            QPushButton:pressed {{
                background-color: {hover_color};
                padding: 11px 9px 9px 11px;
            }}
        """)
        # 设置按钮光标样式
        button.setCursor(Qt.PointingHandCursor)
    def register(self):
        self.window=Register(self)
        self.window.show()
        self.close()
    def login(self):
        self.window=Login(self)
        self.window.show()
        self.close()
    def admin_btn_clicked(self):
        self.window = Admin_Login(self)
        self.window.show()
        self.close()

    def clear_layout(self,layout):
        if layout is None:
            return
        # 从布局中移除所有项目
        while layout.count():
            item = layout.takeAt(0)
            widget = item.widget()
            if widget:  # 如果是控件则删除
                widget.deleteLater()
            else:  # 如果是子布局则递归清除
                self.clear_layout(item.layout())
                item.layout().deleteLater()

    # 创建一带有滑块的容器
    def create_scroll(self,layout):
        scroll_area = QScrollArea()
        scroll_area.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        scroll_area.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
        scroll_area.setWidgetResizable(True)
        frame = QFrame()
        scroll_area.setWidget(frame)
        layout.addWidget(scroll_area)
        return frame
if __name__ == '__main__':
    app = QApplication(sys.argv)
    # 确保中文正常显示
    font = QFont("SimHei")
    app.setFont(font)
    window = Origin()
    window.show()
    sys.exit(app.exec_())
